package pw.goyd.game.api;

import org.apache.ibatis.session.SqlSession;
import pw.goyd.db.mapping.AccountMapper;
import pw.goyd.db.mapping.PlayerMapper;
import pw.goyd.game.model.Account;
import pw.goyd.game.model.Player;
import pw.goyd.game.server.Log;
import pw.goyd.game.server.Server;

import java.util.List;

/**
 * Mapper代理操作数据库
 *
 * @author StanWind
 */
public class DBDone {
    /**
     * 获取account vo
     *
     * @param acc string account
     * @return 账号vo
     */
    public static Account getAccount(String acc) {
        SqlSession session = Server.sessionFactory.openSession();
        AccountMapper accountMapper = session
                .getMapper(AccountMapper.class);                            //	代理获取对象
        Account account = accountMapper.getAccount(acc);
        session.close();
        return account;
    }

    /**
     * 更新账号表记录
     *
     * @param acc 账号vo
     */
    public static void setAccount(Account acc) {
        SqlSession session = Server.sessionFactory.openSession();
        AccountMapper accountMapper = session.getMapper(AccountMapper.class);
        accountMapper.setAccount(acc);
        session.commit();
        session.close();
    }

    /**
     * 验证账号密码
     *
     * @param acc 账号
     * @param pwd 密码
     * @return 账号密码是否匹配
     */
    public static boolean check(String acc, String pwd) {
        Account account = getAccount(acc);
        if (account != null) {
            Log.getLogger().info("Raw-->pwd[" + pwd + "] salt[" + account.getSalt() + "]"
                    + " MD5-->pwd[" + NumberUtils.MD5(pwd) + "]"
                    + " salt[" + NumberUtils.MD5(pwd + account.getSalt()) + "]");
            if (account.getSalt() == null || account.getSalt().equals("")) {
                return account.getPassword_md5().equals(NumberUtils.MD5(pwd));    //	无salt
            } else {
                return account.getPassword_md5().equals(NumberUtils.MD5(pwd + account.getSalt()));    //	计算验证
            }
        } else {
            return false;
        }
    }


    /**
     * 拿到player
     *
     * @param acc 账号
     * @return Player Obj
     */
    public static Player getPlayer(String acc) {
        SqlSession session = Server.sessionFactory.openSession();
        AccountMapper accountMapper = session
                .getMapper(AccountMapper.class);                            //	代理获取对象
        Account account = accountMapper.getAccount(acc);                    //	传入执行
        PlayerMapper playerMapper = session
                .getMapper(PlayerMapper.class);                             //	代理获取对象
        Player player = playerMapper.getPlayer(account.getAccount_id());    //	传入执行
        session.close();
        return player;
    }

    /**
     * 插入一个玩家
     *
     * @param player 玩家vo
     * @return 是否插入成功
     */
    public static boolean insertPlayer(Player player) {
        SqlSession session = Server.sessionFactory.openSession();
        PlayerMapper playerMapper = session.getMapper(PlayerMapper.class);
        playerMapper.insertPlayer(player);
        session.commit();

        //开始查询
        //session = Server.sessionFactory.openSession();
        playerMapper = session.getMapper(PlayerMapper.class);
        boolean state = playerMapper.getPlayer(player.getAccount_id()) != null;
        session.close();
        return state;
    }

    public static void updatePlayer(Player player) {
        SqlSession session = Server.sessionFactory.openSession();
        PlayerMapper playerMapper = session.getMapper(PlayerMapper.class);
        playerMapper.updatePlayer(player);
        playerMapper.updateDiamond(player);
        session.commit();
        session.close();
    }

    public static void updateLucky(Player player) {
        SqlSession session = Server.sessionFactory.openSession();
        PlayerMapper playerMapper = session.getMapper(PlayerMapper.class);
        playerMapper.updateLucky(player);
        playerMapper.updateAD(player);
        session.commit();
        session.close();
    }


    public static boolean isNicknameExist(String nickname) {
        SqlSession session = Server.sessionFactory.openSession();
        PlayerMapper playerMapper = session
                .getMapper(PlayerMapper.class);
        int c = playerMapper.getPlayerCountByNickname(nickname);
        session.close();
        return c != 0;
    }


    public static List<Player> getTopGoldPlayer() {
        SqlSession session = Server.sessionFactory.openSession();
        PlayerMapper playerMapper = session
                .getMapper(PlayerMapper.class);
        List<Player> players = playerMapper.getTopGoldPlayer();
        session.close();
        return players;
    }


}
